import { Order } from 'order-model.js';
var order = new Order();
Page({
  data: {
    statusType: [
      {name: "全部", status: "" }, 
      {name: "待付款", status:1},
      {name: "待发货", status:2}, 
      {name: "待收货", status:3},
      {name: "待评价", status:4}
    ],
    currentTpye:0,
    pageIndex:1,
    isLoadedAll:false,
    loadingHidden:false,
    orderArr:[],
    orderArrAll:[],
    orderArrR:[],
    orderArrDaifahuo: [],
    orderArrYifahuo: [],
    orderArrEvaluate: [],
    page:1,
    searchLoading: false,
    searchLoadingComplete: false,
  },
  onLoad: function (options) {
    this.data.entry = options.entry ? options.entry : 1 ; //入口方式，1个人中心进入，2支付订单时进入
    this.data.index = options.index ? options.index : 0;
    this.data.order_status = this.data.statusType[this.data.index].status;
    this._loadData();
  },
  changeData: function (){
    this._loadData(this.data.currentTpye)
  },

  _loadData: function () {
    this.setData({
      currentTpye: this.data.index
    })
    this._getOrders();
  },

  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    this.data.order_status = this.data.statusType[curType].status;
    this.setData({
      currentTpye: curType,
    });
    this.reset();
    this._getOrders();
  },
  reset: function () {
    this.data.orderArr = [];
    this.setData({
      searchLoadingComplete: false
    })
    wx.pageScrollTo({
      scrollTop: 0
    })
    this.data.page = 1;
  },

  /*全部订单*/
  _getOrders: function () {
    var that = this;
    var params = {
      page: this.data.page,
      status: this.data.order_status
    };
    if (this.data.page == 1) {
      wx.showLoading({
        title: '加载中...',
      })
    }
    this.setData({
      searchLoading: true,
    })
    order.getOrders(params, (res) => {
      if (res.errcode == '401' || res.errcode == 401) {
        that.setData({
          showReload: true
        })
        return false;
      }
      var data = res.items;
      data = this._getGoodsCount(data);
      that.data.orderArr = that.data.orderArr.concat(data);
      that.data.allCount = res._meta.pageCount;
      that.setData({
        showReload: false,
        orderArr: that.data.orderArr,
        loadingHidden: true
      });
      if (res._meta.pageCount <= 1) {
        this.setData({
          searchLoading: false,
          searchLoadingComplete: true,
        })
      }
      if (res.items.length < 1) {
        this.setData({
          searchLoading: false,
          searchLoadingComplete: true,
        })
      }
    },
    (res) => {
      wx.hideLoading();
    });
  },
  _getGoodsCount:function(data){
    for (let i = 0; i < data.length; i++) {
      var goodsCount = 0;
      var childLen = data[i].orderSku.length;
      for (let j = 0; j < childLen; j++) {
        goodsCount = data[i].orderSku[j].num
      }
      data[i].goodsCount = goodsCount;
    }
    return data;
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.currentTpye == 0) {
      var that = this;
      var page = this.data.page + 1;
      if (page <= this.data.pageCount) {
        this.setData({
          searchLoading: true,
        })
        order.getOrders(page, (res) => {
          var data = res.items;
          that.data.orderArrAll.push.apply(that.data.orderArrAll, data);  //数组合并
          that.setData({
            orderArr: that.data.orderArrAll,
            pageCount: res._meta.pageCount,
            page: page,
          });
          if (page == this.data.pageCount) {
            this.setData({
              searchLoading: false,
              searchLoadingComplete: true,
            })
          }

        });
      }

    }
    if (this.data.currentTpye == 1) {
      var that = this;
      var page = this.data.page + 1;
      if (page <= this.data.pageCount) {
        this.setData({
          searchLoading: true,
        })
        order.getOrdersReady(page, (res) => {
          var data = res.items;
          that.data.orderArrR.push.apply(that.data.orderArrR, data);  //数组合并
          that.setData({
            orderArr: that.data.orderArrR,
            pageCount: res._meta.pageCount,
            page: page,
          });
          if (page == this.data.pageCount) {
            this.setData({
              searchLoading: false,
              searchLoadingComplete: true,
            })
          }

        });
      }

    }
    if (this.data.currentTpye == 2) {
      var that = this;
      var page = this.data.page + 1;
      if (page <= this.data.pageCount) {
        this.setData({
          searchLoading: true,
        })
        order.getOrdersDaifahuo(page, (res) => {
          var data = res.items;
          that.data.orderArrDaifahuo.push.apply(that.data.orderArrDaifahuo, data);  //数组合并
          that.setData({
            orderArr: that.data.orderArrDaifahuo,
            pageCount: res._meta.pageCount,
            page: page,
          });
          if (page == this.data.pageCount) {
            this.setData({
              searchLoading: false,
              searchLoadingComplete: true,
            })
          }

        });
      }

    }
    if (this.data.currentTpye == 3) {
      var that = this;
      var page = this.data.page + 1;
      if (page <= this.data.pageCount) {
        this.setData({
          searchLoading: true,
        })
        order.getOrdersYifahuo(page, (res) => {
          var data = res.items;
          that.data.orderArrYifahuo.push.apply(that.data.orderArrYifahuo, data);  //数组合并
          that.setData({
            orderArr: that.data.orderArrYifahuo,
            pageCount: res._meta.pageCount,
            page: page,
          });
          if (page == this.data.pageCount) {
            this.setData({
              searchLoading: false,
              searchLoadingComplete: true,
            })
          }

        });
      }

    }
    if (this.data.currentTpye == 4) {
      var that = this;
      var page = this.data.page + 1;
      if (page <= this.data.pageCount) {
        this.setData({
          searchLoading: true,
        })
        order.getOrdersEvaluate(page, (res) => {
          var data = res.items;
          that.data.orderArrEvaluate.push.apply(that.data.orderArrEvaluate, data);  //数组合并
          that.setData({
            orderArr: that.data.orderArrEvaluate,
            pageCount: res._meta.pageCount,
            page: page,
          });
          if (page == this.data.pageCount) {
            this.setData({
              searchLoading: false,
              searchLoadingComplete: true,
            })
          }
        });
      }

    }

  },

  /*显示订单的具体信息*/
  showOrderDetailInfo: function (event) {
    var id = order.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../order/order?from=order&id=' + id
    });
  },

  toLogisticDetail: function (event){
    var id = order.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../logistics/logistics?id='+id
    });
  },

  toEvaluate: function (event){
    var id = order.getDataSet(event, 'id');
    var shopId = order.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../evaluate/list?order_id='+id+'&shopId='+shopId
    });
  },

  goTodetails:function(event){
    var status = order.getDataSet(event, 'status');
    var id = order.getDataSet(event, 'id');
      wx.navigateTo({
        url: 'details?id='+id+'&status='+status,
      })
  },

  /*未支付订单再次支付*/
  rePay: function (event) {
    var id =  order.getDataSet(event, 'id');
    wx.navigateTo({
      url: 'payment?order_id='+id
    });
  },
  // 删除订单
  _deleteOrder: function (event) {
    var id = order.getDataSet(event, "id");
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除订单吗？',
      confirmText: '确定',
      cancelText: '取消',
      success: function (res) {
        if (res.confirm) {
          order.deleteOrder(id, (res) => {
            if (res.status == 1) {
              wx.showToast({
                title: '订单已删除',
                icon: 'none',
                duration: 1000
              })
              var orderArr = that.data.orderArr;
              for (var i = 0; i < orderArr.length; i++) {
                if (id == orderArr[i].id) {
                  orderArr.splice(i, 1)
                  break;
                }
              }
              that.setData({
                orderArr: orderArr
              })
            }else{
              wx.showToast({
                title: '系统繁忙，请重试一下~',
                icon: 'none',
                duration: 2000
              })
            }

          })
        }
      }
    })
  },

  // 申请退款
  toRefundOrder: function (event){
    var id = order.getDataSet(event, "id");
    wx.navigateTo({
      url: 'refund?id='+id,
    })
  },

// 确认收货
  _confirmOrder: function (event){
    var that = this;
    var id = order.getDataSet(event, "id")
    wx.showModal({
      title: '提示',
      content: '你是否收到该订单商品？',
      confirmText: '已收货',
      cancelText: '未收货',
      success: function (res) {
        if (res.confirm) {
          console.log('已收货')
          order.confirmOrder(id,(res)=>{
            if(res.status==1){
              wx.showToast({
                title: '确认收货成功',
                icon: 'none',
                duration: 2000
              })
              that._loadData(that.data.currentTpye)
            } else {
              wx.showToast({
                title: '系统繁忙，请重试一下~',
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else if (res.cancel) {
          console.log('未收货')
        }
      }
    })
  },

//再次购买
_buyAgain:function(event){
  var id = order.getDataSet(event,'id')
  order.buyAgain(id);
},
//卸载页面
onUnload: function () {
  if (this.data.entry == 2) {
    wx.switchTab({
      url: '../center/center',
    })
  }
},
goHome:function(){
  wx.switchTab({
    url: '/pages/home/home',
  })
}
})